package com.tz.yiqingapp.controller.admin;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tz.yiqingapp.bean.Admin;
import com.tz.yiqingapp.bean.Spiderlog;
import com.tz.yiqingapp.config.AccessLimit;
import com.tz.yiqingapp.mapper.AdminMapper;
import com.tz.yiqingapp.mapper.DoctorMapper;
import com.tz.yiqingapp.mapper.SpiderMapper;
import com.tz.yiqingapp.mapper.UserMapper;
import com.tz.yiqingapp.utils.MyPasswordEncoder;
import com.tz.yiqingapp.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminLogin {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    DoctorMapper doctorMapper;

    @Autowired
    SpiderMapper spiderMapper;

    @AccessLimit(times = 5)
    @PostMapping("/login")
    @ResponseBody
    public HashMap<String,Object> login (@RequestBody Admin admin, HttpServletRequest request) {
        Admin realadmin = adminMapper.selectById(1);
        MyPasswordEncoder passwordEncoder = new MyPasswordEncoder();
        if(admin.getName().equals(realadmin.getName()) && passwordEncoder.matches(admin.getPassword(),realadmin.getPassword()) ){
            request.getSession().setAttribute("loginrole","admin");
            System.out.println("sessionid :" + request.getSession().getId());
            return ResponseUtils.success("login success:" ); // 2021nngdf
        }
        return ResponseUtils.fail("login fail :" + realadmin.getPassword() +" .. " + passwordEncoder.encode(admin.getPassword()));
    }
    @AccessLimit(times = 5)
    @PostMapping("/getDashboardInfo")
    @ResponseBody
    public HashMap<String,Object> getDashboardInfo (@RequestBody Admin admin, HttpServletRequest request) {

        System.out.println("sessionid :" + request.getSession().getId());
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }

        HashMap<String ,Object> hashMap = new HashMap<>();
        hashMap.put("code",1);
        hashMap.put("usercount",userMapper.selectCount(null));
        hashMap.put("doctorcount",doctorMapper.selectCount(null));
        QueryWrapper<Spiderlog> spiderlogQueryWrapper = new QueryWrapper<>();
        spiderlogQueryWrapper.lambda().orderByDesc(Spiderlog::getId);
        List<Spiderlog> spiderlogs = spiderMapper.selectList(spiderlogQueryWrapper);
        if(spiderlogs.size()>0) {
            hashMap.put("spiderinfo",spiderlogs.get(0));
        }
        return hashMap;

    }
    @AccessLimit(times = 5)
    @PostMapping("/logout")
    public HashMap<String,Object> logout (@RequestBody Admin admin, HttpServletRequest request) {
        request.getSession().setAttribute("loginrole",null);
        request.getSession().setAttribute("loginid",null);
        return ResponseUtils.notlogin("login fail ");
    }
    @AccessLimit(times = 5)
    @PostMapping("/changepass")
    public HashMap<String,Object> changepass (@RequestBody Admin admin, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("admin")){
            Admin realadmin = adminMapper.selectById(1);
            MyPasswordEncoder passwordEncoder = new MyPasswordEncoder();
            realadmin.setPassword(passwordEncoder.encode(admin.getPassword()));
            if(adminMapper.updateById(realadmin) >0){
                return ResponseUtils.success("change success");
            }

        }
        return ResponseUtils.notlogin("login fail");
    }
}
